package xuhaijia.lanou3g.autohome.myself;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;
import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.BaseActivity;
import xuhaijia.lanou3g.autohome.db.DBToolForCollection;
import xuhaijia.lanou3g.autohome.findcar.screen.ScreenInActivity;
import xuhaijia.lanou3g.autohome.recommend.originalvideo.VideoPlayerActivity;

/**
 * Created by 7 on 16/9/1.
 */
public class CollectionActivity extends BaseActivity implements View.OnClickListener {

    private TextView clear;
    private ListView lv;
    private CollectionAdapter collectionAdapter;
    private BmobUser bmobUser;
    private BmobQuery<Collection> query;

    @Override
    protected int setLayout() {
        return R.layout.activity_collection;
    }

    @Override
    protected void initView() {
        clear = bindView(R.id.activity_collection_clear);
        clear.setOnClickListener(this);
        lv = bindView(R.id.activity_collection_lv);
    }

    @Override
    protected void initData() {
        collectionAdapter = new CollectionAdapter(this);
        lv.setAdapter(collectionAdapter);
        bmobUser = BmobUser.getCurrentUser();
        query = new BmobQuery<>();
        DBToolForCollection.getInstance().queryIsExist("account = ?", bmobUser.getUsername(), new DBToolForCollection.QueryListener() {
            @Override
            public void onQueryComplete(final ArrayList<Collection> mCollections) {
                if (mCollections.size() == 0) {
                    query.addWhereEqualTo("account", bmobUser.getUsername());
                    query.setLimit(50);
                    query.findObjects(new FindListener<Collection>() {
                        @Override
                        public void done(List<Collection> list, BmobException e) {
                            if (e == null) {
                                for (int i = 0; i < list.size(); i++) {
                                    DBToolForCollection.getInstance().insertCollection(list.get(i));
                                    Log.d("CollectionActivity", "存入本地数据库");
                                }
                                Collections.sort(list ,new CollectionOrder());
                                collectionAdapter.setArrayList((ArrayList<Collection>) list);
                            } else {
                                Toast.makeText(CollectionActivity.this, "该用户还没有任何收藏信息", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                } else {
                    Collections.sort(mCollections, new CollectionOrder());
                    collectionAdapter.setArrayList(mCollections);
                    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            String url = mCollections.get(position).getUrl();
                            Intent intent = new Intent(CollectionActivity.this, ScreenInActivity.class);
                            intent.putExtra("url", url);
                            startActivity(intent);
                        }
                    });
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_collection_clear:
                DBToolForCollection.getInstance().deleteCollectionBy("account = ? ", bmobUser.getUsername());
                DBToolForCollection.getInstance().queryIsExist("account = ? ", bmobUser.getUsername(), new DBToolForCollection.QueryListener() {
                    @Override
                    public void onQueryComplete(ArrayList<Collection> mCollections) {

                        collectionAdapter.setArrayList(mCollections);
                    }
                });
                BmobQuery<Collection> query = new BmobQuery<>();
                query.addWhereEqualTo("account", bmobUser.getUsername());
                query.setLimit(50);
                query.findObjects(new FindListener<Collection>() {
                    @Override
                    public void done(List<Collection> list, BmobException e) {
                        if (e == null) {
                            Log.d("CollectionActivity", "查询成功");
                            for (int i = 0; i < list.size(); i++) {
                                Collection collection = new Collection();
                                collection.setObjectId(list.get(i).getObjectId());
                                collection.delete(new UpdateListener() {
                                    @Override
                                    public void done(BmobException e) {
                                        if (e == null) {
                                            Toast.makeText(CollectionActivity.this, "清空云端成功", Toast.LENGTH_SHORT).show();
                                        } else {
                                            Toast.makeText(CollectionActivity.this, "清空云端失败", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }
                        } else {
                            Log.d("CollectionActivity", "查询失败");
                        }
                    }
                });
                break;
        }
    }

    private class CollectionOrder implements Comparator {
        @Override
        public int compare(Object lhs, Object rhs) {
            Collection p1 = (Collection) lhs;
            Collection p2 = (Collection) rhs;
            return new Long(p2.getLookTime()).compareTo(new Long(p1.getLookTime()));
        }
    }
}
